package com.cate.action;

import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;  
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.cate.bean.User;
import com.cate.service.UserService;
import com.cate.util.ResultMap;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

  
@Controller  
public class UserController {  
	@Autowired
	UserService userService;
	Logger logger = Logger.getLogger(UserController.class);
    @RequestMapping(value="/getUserInfo")  
    public String getUserInfo(HttpServletRequest request){  
    	User currentUser = (User)request.getSession().getAttribute("currentUser");  
        System.out.println("当前登录的用户为[" + currentUser + "]");  
        request.setAttribute("currUser", currentUser.getUsername());  
        return "/info";  
    }  
    
    @RequestMapping("queryAll")
	@ResponseBody
	public  ModelAndView queryAll(String cur_page,HttpServletRequest request,HttpServletResponse response){
		if(cur_page == null){
			cur_page = "1";
		}
		logger.info(cur_page);
		int curPage = Integer.parseInt(cur_page);
		int limit = 3;
//		int start = (curPage - 1)*limit+1;
//		int end = curPage*limit;
		HashMap<String,Object>  wherMap= new HashMap<String,Object>();
//		wherMap.put("start", start);
//		wherMap.put("end", end);
		wherMap.put("orderbysort", " publishdate desc");
		wherMap.put("cur_page", cur_page);
		PageHelper.startPage(curPage, limit);
		List<ResultMap<String,Object>> books = userService.queryBooks(wherMap);
		PageInfo<ResultMap<String, Object>> page = new PageInfo<ResultMap<String, Object>>(books);
		logger.info(page);
		ModelAndView mode = new ModelAndView("views");
		mode.addObject("pager", page);
		return mode;
	}
    
    @RequestMapping("queryProduct")  
    @ResponseBody
    public List<ResultMap<String, Object>> queryProduct(HttpServletRequest request){  
    	HashMap<String,Object>  wherMap= new HashMap<String,Object>();
    	List<ResultMap<String, Object>> product = userService.queryProduct(wherMap);
    	String str = JSON.toJSONString(product);
    	logger.info(str);
        return product;  
    }  
    @RequestMapping("queryMenu")  
    @ResponseBody
    public List<ResultMap<String, Object>> queryMenu(HttpServletRequest request,String product_no){  
    	HashMap<String,Object>  wherMap= new HashMap<String,Object>();
    	wherMap.put("product_no", product_no);
    	List<ResultMap<String, Object>> product = userService.queryMenu(wherMap);
    	String str = JSON.toJSONString(product);
    	logger.info(str);
        return product;  
    } 
}  